﻿using Company.Sqlite.Interfaces;
using Company.Sqlite.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Company.Sqlite.Repositories
{
    public class CarRepository : RepositoryBase, ICarRepository
    {
        public int Delete(Car entity)
        {
            db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
            return db.SaveChanges();
        }

        public Car Get(int id)
        {
            return db.Cars.Find(id);
        }

        public List<Car> GetAll()
        {
            return db.Cars.ToList();
        }

        public List<Car> GetAll(string keyword)
        {
            return db.Cars.ToList().Where(t => t.Name.Contains(keyword)).ToList();
        }

        public int Insert(Car entity)
        {
            db.Cars.Add(entity);
            return db.SaveChanges();
        }

        public Car Select(string keyword)
        {
            return db.Cars.ToList().Find(t => t.Name == keyword);
        }

        public int Update(Car entity)
        {
            db.Entry(entity).State = System.Data.Entity.EntityState.Modified;
            return db.SaveChanges();
        }
    }
}
